<?php
/**
 * Name: AdminUserModel.class.php
 * Description:
 * User: DIY
 * Date: 2016/9/8
 * Time: 18:09
 */

namespace Common\Model;


use Think\Model;

class AdminUserModel extends Model{

    //自动完成
    protected $_auto = array (
        array('password','md5',3,'function'),//新增 和 编辑
        array('last_login_time','time',3,'function'),//新增 和 编辑
        array('last_login_ip','get_client_ip',3,'function'),//新增 和 编辑
    );

    /*
     * 通过用户ID查找用户
     * */
    public function getUserByID($id){
        return $this->where(array('id'=>$id))->find();
    }

    public function getUserByName($username){

        return $this->where(array('username'=>$username))->find();
    }

    /*
 * 取得用户列表
 * @param data=array() 查询数组条件
 * @param page 页数
 * @param number 条数
 * return array()
 * */
    public function getAdminUserList($data = array(),$limit=20){
        //默认参数
        $param = array(
            '1'=>'1'
        );
        //组合查询参数
        if(isset($data['username']) && $data['username'] != ''){
            $param['p1.username'] = array('like','%'.$data['username'].'%');
        }
        if(isset($data['realname']) && $data['realname'] != ''){
            $param['p1.realname'] = array('like','%'.$data['realname'].'%');
        }
        if(isset($data['phone']) && $data['phone'] != ''){
            $param['p1.phone'] = $data['phone'];
        }
        if(isset($data['status']) && $data['status'] != ''){
            $param['p1.status'] = $data['status'];
        }
        $list = $this
            ->alias('p1')
            ->field('p1.*,p2.name as role_name')
            ->join('LEFT JOIN __ADMIN_ROLE__ p2 ON p2.id = p1.role_id')
            ->where($param)
            ->limit($limit)
            ->select();
        return $list;

    }

    /*
     * 获取总用户数
     * @param data=array() 查询数组条件
     * @param data=array() 查询数组条件
     * */
    public function getAdminUserCount($data = array()){
        //默认参数
        $param = array(
            '1'=>'1'
        );
        //组合查询参数
        if(isset($data['username']) && $data['username'] != ''){
            $param['p1.username'] = array('like','%'.$data['username'].'%');
        }
        if(isset($data['realname']) && $data['realname'] != ''){
            $param['p1.realname'] = array('like','%'.$data['realname'].'%');
        }
        if(isset($data['phone']) && $data['phone'] != ''){
            $param['p1.phone'] = $data['phone'];
        }
        if(isset($data['status']) && $data['status'] != ''){
            $param['p1.status'] = $data['status'];
        }
        $count = $this->alias('p1')->where($param)->count();
        return $count;
    }

}